package com.tuniu.agents.common;

import com.tuniu.agents.advisor.ChatEnvironmentMemory;
import io.micrometer.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

import java.util.Locale;
import java.util.Map;
import java.util.Objects;

@Slf4j
@Component
public class I18nServiceImpl implements I18nService {

    @Autowired
    private MessageSource messageSource;
    @Autowired
    private ChatEnvironmentMemory chatEnvironmentMemory;

    public String getMessageForConversation(String conversationId, String code, Object[] args, String defaultMessage) {
        // Get the locale for the conversation, or use the default locale if not found
        Locale locale = getLocaleForConversation(conversationId) == null ? getLocale() : getLocaleForConversation(conversationId);
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }

    public Locale getLocaleForConversation(String conversationId) {
        // Check if the conversation ID is empty
        if (StringUtils.isEmpty(conversationId)){
            return null;
        }
        Locale locale = CustomLocaleContextHolder.getLocale(conversationId);
        if (locale == null) {
            String languageTag = (String) chatEnvironmentMemory.get(conversationId, AgentsConstants.AgentsConversationEnvironmentKey.ACCEPT_LANGUAGE);
            if (StringUtils.isNotEmpty(languageTag)) {
                locale = Locale.forLanguageTag(languageTag);
                CustomLocaleContextHolder.setLocale(conversationId, locale);
            } else {
                locale = getLocale();
            }
        }
        return locale;
    }

    @Override
    public void setLocaleForConversation(String conversationId, Locale locale) {
        CustomLocaleContextHolder.setLocale(conversationId, locale);
        chatEnvironmentMemory.add(conversationId, Map.of(AgentsConstants.AgentsConversationEnvironmentKey.ACCEPT_LANGUAGE, locale.toLanguageTag()));
    }

    @Override
    public String getMessage(String code, Object[] args, String defaultMessage) {
        return getMessage("", code, args, defaultMessage);
    }

    @Override
    public String getMessage(String code, Object[] args, String defaultMessage,Locale locale) {
        if (Objects.isNull(locale)){
            locale=getLocale();
        }
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }

    @Override
    public String getMessage(String localeContextId, String code, Object[] args, String defaultMessage) {
        Locale locale = getLocale(localeContextId) == null ? getLocale() : getLocale(localeContextId);
        return messageSource.getMessage(code, args, defaultMessage, locale);
    }


    public Locale getLocale(String localeContextId) {
        return CustomLocaleContextHolder.getLocale(localeContextId);
    }

    @Override
    public void setLocale(String localeContextId, Locale locale) {
        CustomLocaleContextHolder.setLocale(localeContextId, locale);
    }

    public Locale getLocale() {
        return LocaleContextHolder.getLocale();
    }
}
